//
//  FindRoot.hpp
//  Cpp_Demo
//
//  Created by Robin Guo on 2017/9/23.
//  Copyright © 2017年 Robin Guo. All rights reserved.
//

#ifndef FindRoot_hpp
#define FindRoot_hpp

#include <stdio.h>
#include <cmath>
#include <iostream>

using namespace std;

/*
 求解一元二次方程：d = b*b - 4ac;
 d > 0: 2个 值
 x1 = (-b + sqrt(d))/2a;
 x2 = (-b - sqrt(d))/2a;

 d = 0: 2个相等的值
 x1 = x2 = (-b)/2a
 
 d < 0: 2 个虚数值
 x1 = -b/2a
 x2 = sqrt(-b)/2a
 */
class FindRoot {
private:
    float a,b,c,d;
    double x1,x2;
    
public:
    FindRoot(float x,float y,float z) {
        a=x;b=y;c=z;
        d = b*b - 4*a*c;
    }
    
    void Find() {
        if (d > 0) {
            x1 = (-b + sqrt(d))/2*a;
            x2 = (-b - sqrt(d))/2*a;
            return;
        }else if (d == 0) {
            x1 = x2 = (-b)/2 * a;
        }else{
            x1 = (-b)/2*a;
            x2 = sqrt(-b)/2*a;
        }
    }
    
    void Display() {
        if (d > 0) {
            cout << "x1 = " << x1 << endl;
            cout << "x2 = " << x2 << endl;
        }else if (d == 0) {
            cout << "x1 = x2 = " << x1 << endl;
        }else {
            cout << "x1 = " << x1 << " + " << x2 << "i" << endl;
            cout << "x2 = " << x1 << " - " << x2 << "i" << endl;
        }
    }
    
};
#endif /* FindRoot_hpp */
